package com.karina.visualization.chart.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChartModelUtil {

	/**
	 * the util try to create list of Map which can be used by Trinity Chart the
	 * chart
	 * 
	 * string data format will be
	 * 
	 * {[key: value, key2: value, key3: value3, key4:value4], [key: valuea,
	 * key2: valuea, key3: valuea: key4:value4] }
	 * 
	 * @param data
	 * @return
	 */

	public static List<Map<String, Object>> createChartDataProvider(String data) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		int leftIndex = data.indexOf('{');
		int rightIndex = data.lastIndexOf('}');
		String value = data;
		if (leftIndex != -1 && rightIndex != -1) {
			value = value.substring(leftIndex + 1, rightIndex);
		}

		String[] array = value.split("[\\[|\\]]");

		for (String str : array) {
			if (!str.trim().equals(""))
				addContentToList(list, str);
		}

		return list;
	}

	public static void addContentToList(List<Map<String, Object>> map, String dt) {

		Map<String, Object> v = new HashMap<String, Object>();
		String[] dataArray = dt.split(",");

		for (String entry : dataArray) {
			String[] entryPair = entry.split(":");
			v.put(entryPair[0].trim(), entryPair[1].trim());
		}
		map.add(v);

	}
}
